package com.open.party.cloud.view.home.jiCengDangJian.partyService

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppRecycleViewTitleBaseActivity
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.ZyzTuanDuiEntity
import com.open.party.cloud.view.home.jiCengDangJian.adapter.PartyTuanDuiListAdapter
import com.open.party.cloud.viewModel.ZiYuanZheViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ZyzTuanDuiListActivity : AppRecycleViewTitleBaseActivity<ZyzTuanDuiEntity>(), OnSuperListener<ZyzTuanDuiEntity> {

    private var ziYuanZheVM: ZiYuanZheViewModel? = null
    private var adapter: PartyTuanDuiListAdapter? = null

    private var parentId = ""

    override fun getLayoutResId(): Int =
        R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_none
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("服务团队")
        ziYuanZheVM = ZiYuanZheViewModel()
        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = PartyTuanDuiListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.showLoading("努力加载中")

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
        refreshData()
    }

    override fun loadData(pageVo: PageVo<ZyzTuanDuiEntity>) {
        val pageReq = PageReq<ZyzTuanDuiEntity>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = 7

        val vo = ZyzTuanDuiEntity()
        vo.recordType = 1
//        vo.parentId = parentId

        pageReq.data = vo
        ziYuanZheVM!!.getZyzTuanDuiListData(pageReq)
    }

    override fun onClick(position: Int, itemData: ZyzTuanDuiEntity, flag: String?) {
        XUtils.intent()
            .openActivity(this@ZyzTuanDuiListActivity, ZyzTuanDuiDetailActivity::class.java)
            .putStringExtra("id", itemData.id)
            .putStringExtra("name", itemData.orgName)
            .requestCode(100)
            .start()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ZyzTuanDuiEntity>>) {
        if (result.eventType != "getZyzTuanDuiListData") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<ZyzTuanDuiEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
        stopLoading(loadingRecyclerView, loadType)
    }

//    private fun initTopView() {
//
//    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public fun eventBus2Callback(result: ResultInfo<ArrayList<OrgTreeBean>>) {
//        if (result.eventType != "loadOrgList") {
//            return
//        }
//
//        when (result.code) {
//            ResultInfo.SUCCESS -> {
//                if (result.data != null) {
//                    showOrgView(result.data)
//                } else {
//                }
//            }
//            ResultInfo.TOKEN_OVERDUE -> {
//            }
//            else -> {
//            }
//        }
//    }


//    var l1ListData: ArrayList<OrgTreeBean>? = null
//    var l2ListData: ArrayList<OrgTreeBean>? = null
//    var l3ListData: ArrayList<OrgTreeBean>? = null

//    private fun showOrgView(orgTree: ArrayList<OrgTreeBean>) {
//        l1ListData = orgTree
//
//        val l1List = ArrayList<String>()
//        for (item in l1ListData!!) {
//            l1List.add(item.orgName)
//        }
//
//        l1List.add(0, "全部")
//        if (l1List.size > 0) {
//            initSpinnerView(type1Spinner, l1List, object : AdapterView.OnItemSelectedListener {
//                override fun onNothingSelected(parent: AdapterView<*>?) {
//                }
//
//                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position1: Int, id: Long) {
//                    if (view != null) {
//                        val tv = view as TextView
//                        tv.setTextColor(ContextCompat.getColor(this@PartyTuanDuiListActivity, R.color.page_title))
//                        tv.paint.textSize = UIUtil.sp2px(this@PartyTuanDuiListActivity, 14f).toFloat()
//
//                        if (position1 == 0) {
//                            parentId = ""
//                            refreshData()
//
//                            val l2List = ArrayList<String>()
//                            l2List.add(0, "全部")
//                            initSpinnerView(type2Spinner, l2List, null)
//
//                            val l3List = ArrayList<String>()
//                            l3List.add(0, "全部")
//                            initSpinnerView(type3Spinner, l3List, null)
//                        } else {
//                            val l2Data = l1ListData!![position1 - 1]//
//                            if (l2Data.children == null || l2Data.children.size == 0) {
//                                parentId = l2Data.id
//                                refreshData()
//                                initSpinnerView(type2Spinner, ArrayList(), null)
//                                initSpinnerView(type3Spinner, ArrayList(), null)
//                            } else {
//                                l2ListData = l2Data.children as ArrayList<OrgTreeBean>?
//                                val l2List = ArrayList<String>()
//                                for (item in l2ListData!!) {
//                                    l2List.add(item.orgName)
//                                }
//
//                                if (l2List.size > 0) {
//                                    initSpinnerView(type2Spinner, l2List, object : AdapterView.OnItemSelectedListener {
//                                        override fun onNothingSelected(parent: AdapterView<*>?) {
//                                        }
//
//                                        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//                                            if (view != null) {
//                                                val tv2 = view as TextView
//                                                tv2.setTextColor(ContextCompat.getColor(this@PartyTuanDuiListActivity, R.color.page_title))
//                                                tv2.paint.textSize = UIUtil.sp2px(this@PartyTuanDuiListActivity, 14f).toFloat()
//
//                                                val l3Data = l2ListData!![position]
//                                                if (l3Data.children == null || l3Data.children.size == 0) {
//                                                    parentId = l3Data.id
//                                                    refreshData()
//                                                    initSpinnerView(type3Spinner, ArrayList(), null)
//                                                } else {
//                                                    l3ListData = l3Data.children as ArrayList<OrgTreeBean>?
//                                                    val l3List = ArrayList<String>()
//                                                    for (item in l3ListData!!) {
//                                                        l3List.add(item.orgName)
//                                                    }
//
//                                                    if (l3List.size > 0) {
//                                                        initSpinnerView(type3Spinner, l3List, object : AdapterView.OnItemSelectedListener {
//                                                            override fun onNothingSelected(parent: AdapterView<*>?) {
//                                                            }
//
//                                                            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
//                                                                if (view != null) {
//                                                                    val tv3 = view as TextView
//                                                                    tv3.setTextColor(ContextCompat.getColor(this@PartyTuanDuiListActivity, R.color.page_title))
//                                                                    tv3.paint.textSize = UIUtil.sp2px(this@PartyTuanDuiListActivity, 14f).toFloat()
//
//                                                                    val l4Data = l3ListData!![position]
//                                                                    parentId = l4Data.id
//                                                                    refreshData()
//                                                                }
//                                                            }
//                                                        })
//                                                    }
//                                                }
//                                            }
//                                        }
//                                    })
//                                }
//                            }
//                        }
//                    }
//                }
//            })
//        }
//    }
//    var spinnerAdapter: ArrayAdapter<String>? = null
//
//    private fun initSpinnerView(
//        spinner: AppCompatSpinner, streetStrList: ArrayList<String>, itemSelectedListener: AdapterView.OnItemSelectedListener?
//    ) {
//        spinnerAdapter = ArrayAdapter(this, R.layout.street_spinner_item, streetStrList!!)
//        spinnerAdapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        spinner.adapter = spinnerAdapter
//        spinner.dropDownVerticalOffset = 100
//
//        spinner.onItemSelectedListener = itemSelectedListener
//    }


}